# -*- coding: iso-8859-1 -*-
#
#    This is part of Malvinas Argentinas Messenger
#
#    Malvinas Argentinas Messenger is free software; you can redistribute
#    it and/or modify it under the terms of the GNU General Public License
#    as published by the Free Software Foundation; either version 2 of the
#    License, or (at your option) any later version.
#
#    Malvinas Argentinas Messenger is distributed in the hope that it will be
#    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Malvinas Argentinas Messenger; if not, write to the Free
#    Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
#    MA  02110-1301  USA

import curses
import win

# This option affects the pads in option windows
STORED_SCREENS = 6

class Pad(win.Win):
	def __init__(self, rows, cols, y, x):
		win.Win.__init__(self, rows, cols, y, x)
		self.pRows = rows * STORED_SCREENS
		self.p = curses.newpad(self.pRows, cols)
		self.p.scrollok(1)
		self.scroll = 0
		self.text = []
		self.id = ''
    
	def addch(self, char):
		self.p.addch(char)

	def addstr(self, text, attr = None):
		self.text.append((text, attr))

		try:
			if attr:
				self.p.addstr(text, attr)
			else:
				self.p.addstr(text)
		except curses.error, e:
			pass
			
		(padY, padX) = self.p.getyx()
		(scrY, scrX) = self.p.getmaxyx()

		if padY >= (scrY - 1):
			self.pRows *= 2
			self.p = curses.newpad(self.pRows, scrX)
			self.redraw()

	def dump(self, file):
		for elem in self.text:			
			file.write(elem[0])
		file.close()    

	def resize(self, sizeY, sizeX, y, x):
		self.p = curses.newpad(self.pRows, sizeX)
		self.x = x
		self.y = y
		self.rows = sizeY
		self.cols = sizeX
		self.redraw()

	def refresh(self):
		(y, x) = self.p.getyx()
		firstRow = y - (self.rows - 1)
		if (firstRow + self.scroll) < 0:
			firstRow = 0
		sizeY = self.y + self.rows - 1
		(padY, padX) = self.p.getyx()
		try:
			self.p.refresh(firstRow + self.scroll, 0, self.y, self.x,\
			self.y+self.rows-1, self.cols)
		except curses.error:
			pass
	    
	def redraw(self):
		self.p.erase()		
		try:
			for elem in self.text:
				if elem[1] == None:
					self.p.addstr(elem[0])
				else:
					self.p.addstr(elem[0], elem[1])
		except:
			pass

	def attrset(self, color):
		self.p.attrset(color)

	def toTop(self):
		(y, x) = self.p.getyx()
		self.scroll = -(y - self.rows + 1) 

	def toBottom(self):
		self.scroll = 0

	def line(self):
		(y, x) = self.p.getyx()
		(maxY, maxX) = self.p.getmaxyx()
		self.p.hline(y, x, curses.ACS_HLINE, maxX - x)

	def scrollUp(self, lines = 1):
		(y, x) = self.p.getyx()
		firstRow = y - self.rows
		if (firstRow + self.scroll - lines) >= 0:
			self.scroll -= lines
		else:
			self.scroll = - (y - self.rows + 1)
		self.refresh()
		
	def getWin(self):
		return self.w
		
	def erase(self):
		self.text = []
		self.p.erase()
		#self.refresh()
		
	def scrollDown(self, lines = 1):
		#(y, x) = self.p.getyx()
		#if (self.scroll + lines) < 0:
		self.scroll += lines
		#else:
		#	self.scroll = 0
		self.refresh()
